package cloud.take.off.module.common;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

import java.util.List;
@Data
@ToString
@NoArgsConstructor
@ApiModel(value = "list返回包装类", description = "list返回包装类")
public class ListResult<T> extends ResponseResult  {
    public ListResult(ResultCode resultCode) {
        super(resultCode);
    }
    public ListResult(ResultCode resultCode,int total) {
        super(resultCode);
        this.total=total;
    }
    public ListResult(List<T> list, ResultCode resultCode, int total) {
        super(resultCode);
        this.list = list;
        this.total=total;
    }
    @ApiModelProperty(value = "数据列表")
    private List<T> list;
    @ApiModelProperty(value = "总条数")
    private int total;
    @ApiModelProperty(value = "备注")
    private String remark;

    public static <T> ListResult<T> data(IPage page, List<T> data) {
        ListResult<T> result = new ListResult(CommonCode.SUCCESS);
        result.setList(data);
        result.setTotal(Long.valueOf(page.getTotal()).intValue());
        return result;
    }

    public static <T> ListResult<T> data(List<T> data) {
        ListResult<T> result = new ListResult(CommonCode.SUCCESS);
        result.setList(data);
        result.setTotal(CollectionUtil.isNotEmpty(data) ? data.size() : 0);
        return result;
    }

    public static <T> ListResult<T> fail() {
        return new ListResult(CommonCode.FAIL);
    }
}
